use simplelog::*;
use std::fs::{self, OpenOptions};
use std::path::PathBuf;
use chrono::Local;
use dirs::home_dir;

pub fn init_logger() {
    let log_path = get_log_file_path();
    if let Some(parent) = log_path.parent() {
        let _ = fs::create_dir_all(parent);
    }
    
    let file = OpenOptions::new().append(true).create(true).open(&log_path).unwrap();
    let config = ConfigBuilder::new()
        .set_thread_level(LevelFilter::Info)
        .set_target_level(LevelFilter::Info)
        .set_location_level(LevelFilter::Info)
        .build();
    
    let _ = WriteLogger::init(LevelFilter::Info, config, file);
}

fn get_log_file_path() -> PathBuf {
    let home: PathBuf = home_dir().unwrap_or_else(|| PathBuf::from("."));
    let date: String = Local::now().format("%Y-%m-%d").to_string();
    home.join(".utopia").join("log").join("im-select").join(format!("{}.log", date))
} 